
#import "HeaderScroll.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>

#pragma mark - 滚动头
@interface HeaderScroll ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collection;

@property (nonatomic, strong) NSArray *modelList;

@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@end

@implementation HeaderScroll

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.modelList = [NSArray array];
        [self addSubview:self.collection];
    }
    return self;
}

- (void)setScrollModleList:(NSArray *)list {
    [self layoutIfNeeded];
    self.modelList = list;
    self.collection.frame = self.bounds;
    self.flowLayout.itemSize = self.bounds.size;
    [self.collection reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.modelList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HeaderScrollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    if (self.modelList.count > 0 && indexPath.row < self.modelList.count) {
        AdoptDetail *model = self.modelList[indexPath.row];
        [cell setModle:model];
    }
    [cell setClickImageBlock:^(AdoptDetail *model) {
        if (self.imageBlock) {
            self.imageBlock(model);
        }
    }];
    [cell setClickAVPlayerBlock:^(AdoptDetail *model) {
        if (self.AVPlayerBlock) {
            self.AVPlayerBlock(model);
        }
    }];
    return cell;
}

- (UICollectionView *)collection {
    if (!_collection) {
        self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        self.flowLayout.minimumLineSpacing = 0.0;
        self.flowLayout.minimumInteritemSpacing = 0.0;
        self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collection = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.flowLayout];
        _collection.delegate = self;
        _collection.dataSource = self;
        _collection.pagingEnabled = YES;
        _collection.showsHorizontalScrollIndicator = NO;
        _collection.backgroundColor = [UIColor whiteColor];
        [_collection registerClass:[HeaderScrollCell class] forCellWithReuseIdentifier:@"cellId"];
    }
    return _collection;
}

@end

#pragma mark - cell
@interface HeaderScrollCell ()

@property (nonatomic, strong) UIImageView *imgView;

@property (nonatomic, strong) AVPlayerViewController *playerVC;

@property (nonatomic, strong) UIControl *imgControl;

@property (nonatomic, strong) UIControl *playerControl;

@property (nonatomic, weak) AdoptDetail *scrModel;

@end

@implementation HeaderScrollCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.imgView];
        [self.contentView addSubview:self.playerVC.view];
        [self.contentView addSubview:self.playerControl];
    }
    return self;
}

- (void)setModle:(AdoptDetail *)model {
    self.scrModel = model;
    if ([model.materialType intValue]== 2) {
        self.imgView.hidden = YES;
        self.playerVC.view.hidden = NO;
        self.playerControl.hidden = NO;
        AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:model.materialName]];
        self.playerVC.player = player;
    } else {
        self.imgView.hidden = NO;
        self.playerVC.view.hidden = YES;
        self.playerControl.hidden = YES;
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.materialName] placeholderImage:[UIImage imageNamed:@"placeholder_large"]];
    }
}

- (void)handleTapImage:(UIControl *)sender {
    if (self.clickImageBlock) {
        self.clickImageBlock(self.scrModel);
    }
}

- (void)handleTapPlayer:(UIControl *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        [self.playerVC.player play];
    } else {
        [self.playerVC.player pause];
    }
    if (self.clickAVPlayerBlock) {
        self.clickAVPlayerBlock(self.scrModel);
    }
}
- (UIImageView *)imgView {
    if (!_imgView) {
        _imgView = [[UIImageView alloc] initWithFrame:self.bounds];
        _imgView.userInteractionEnabled = YES;
        self.imgControl = [[UIControl alloc] init];
        self.imgControl.frame = _imgView.bounds;
        [self.imgControl addTarget:self action:@selector(handleTapImage:) forControlEvents:UIControlEventTouchUpInside];
        [_imgView addSubview:self.imgControl];
    }
    return _imgView;
}

- (AVPlayerViewController *)playerVC {
    if (!_playerVC) {
        _playerVC = [[AVPlayerViewController alloc] init];
        _playerVC.view.frame = self.bounds;
    }
    return _playerVC;
}

- (UIControl *)playerControl {
    if (!_playerControl) {
        _playerControl = [[UIControl alloc] init];
        _playerControl.frame = CGRectMake(40.0, 30.0, self.bounds.size.width - 80.0, self.bounds.size.height - 60.0);
        [_playerControl addTarget:self action:@selector(handleTapPlayer:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playerControl;
}

@end

